package liangtWorkSpace.strategy.comparableFile;

/**
 * @ClassName Sorter
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/4/10 17:31
 */

/**
 * 快速排序
 */
public class Sorter {

    /**
     * 让自己实现CompareTo实现任何数组的排序
     * @param arrs
     */
    public static void sort(Comparable[] arrs){
        for (int i = 0;i < arrs.length;i++){
            int minPos = i;
            for ( int j = i+1;j < arrs.length;j++){
                minPos = arrs[j] .comparetTo(arrs[minPos]) == -1 ? j:minPos;
            }
            swap(arrs,i,minPos);
        }
    }
    private static void swap(Comparable arr[] ,int i,int j){
        Comparable tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
    public int[] sort(int[] arrs){
        for (int i = 0;i < arrs.length;i++){
            int minPos = i;
            for ( int j = i+1;j < arrs.length;j++){
                minPos = arrs[j] < arrs[minPos] ? j:minPos;
            }
            swap(arrs,i,minPos);
        }
        return arrs;
    }
    private static void swap(int arr[] ,int i,int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public Cat[] sort(Cat[] arrs){
        for (int i = 0;i < arrs.length;i++){
            int minPos = i;
            for ( int j = i+1;j < arrs.length;j++){
                minPos = arrs[j].weight < arrs[minPos].weight? j:minPos;
            }
            swap(arrs,i,minPos);
        }
        return arrs;
    }
    private static void swap(Cat arr[] ,int i,int j){
        Cat tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
